<?php
//******************************************************
function insert($table,$fields_arr) {
  global $dbconn;
  if (!$dbconn->connect_id) { echo "error"; return false; }
  $strfields="";
  $strvalues="";
  list($key, $val) = each($fields_arr);
  if(is_string($key))
	{
	$strfields = " ($key";
	$strvalues= $val;
	while(list($key, $val) = each($fields_arr))
		{
		$strfields.= ", $key";
		$strvalues.= ",". $val;
		}
	$strfields.=")";
	}
  else
	{
	$strvalues=$fields_arr[0];
    for($i=1;$i<count($fields_arr);$i++)
		{
	      $strvalues .= ", $fields_arr[$i]";
		}
  }
  

	$query = "INSERT INTO $table $strfields VALUES ($strvalues)";
	//echo "insert_sql:<br>$query<br>";
	$dbconn->execute($query);
	//echo mysql_error();
	$insert_id = $dbconn->last_id();
	return $insert_id;
	//return true; //Duc Manh tat
}

function update($table,$id,$valueid,$fields_arr,$where_ext="") {
	global $dbconn;
	if (!$dbconn->connect_id) { return false; }
	list($key, $val) = each($fields_arr);
	$strset=" $key = $val";
	while(list($key, $val) = each($fields_arr)){
		$strset .= ", $key = $val";
	}
	
	$query = "UPDATE $table SET
			$strset
		   WHERE $id='$valueid' $where_ext"; 
	//echo $query."<br>";
	$dbconn->execute($query);
	//if (!$result) { return false; }
	//return true;
}

function delete_rows($table,$fields_arr,$where_ext="") {
	global $dbconn;
	if (!$dbconn->connect_id) { return false; }
	$strwhere = "";
	if(count($fields_arr)>0){
		list($key, $val) = each($fields_arr);
		$strwhere=" $key = $val";
		while(list($key, $val) = each($fields_arr)){
			$strwhere .= "OR $key = $val";
		}
	}
	
	$query = "DELETE FROM $table
			WHERE $strwhere $where_ext";      
	//echo $query;
	$dbconn->execute($query);
}

function count_record($table,$where_ext = ""){
  global $conf;
  if (!$conf->connection) { return false; }
  $query = "SELECT count(*) FROM $table ";
  if($where_ext != "")
	  $query.= " WHERE " . $where_ext;
  #echo $query,"<br>"; 
  $result = mysql_db_query($conf->dbname, $query, $conf->connection);
  if ($result) {
    $rows = mysql_fetch_row($result);
    return $rows[0];
  } else {
    return 0;
  }

}

function query_get($query) {
  global $conf;
  if (!$conf->connection) { return false; }
  $result = mysql_db_query($conf->dbname, $query, $conf->connection);
  if ($result) {
  	$return = array();
    while ($rows = mysql_fetch_array($result)) {
      $return[] = $rows;
    }
    return $return;
  } else {
    return false;
  }
}

function query_get_list($query, $begin, $limit,$where_ext="", $orderby = "") {
  global $conf;
  if (!$conf->connection) { return false; }
  $return=array();
  $query1 = $query;
  if($where_ext <> "")
	$query1 .= " WHERE " .$where_ext;
  
  if($orderby !="") 
	  $query1.= $orderby;

  if($begin >=0 && $limit > 0)
	 $query1 .=" LIMIT $begin, $limit ";
  $result = mysql_db_query($conf->dbname, $query1, $conf->connection);
  if ($result) {
    while ($rows = mysql_fetch_array($result)) {
      $return[] = $rows;
    }
	return $return;
  } else {
    return false;
  }
}
?>